package com.msu.nava.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.Image;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.msu.nava.MainActivity;
import com.msu.nava.R;
import com.msu.nava.adpter.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends BaseActivity{
  private ViewPager vp;
  private ViewPagerAdapter vpAdpter;
  private List<View> views;
  private Button startbtn;
  private CheckBox checkBox;
  private int width;
  private int height;
  //private int screenWidth;
 // private int screenHeight;

  private PopupWindow popupWindow;
  private View popupView;
  private ImageView imageView;
  private boolean isFirstUse;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    SharedPreferences sp = getSharedPreferences("demo",Context.MODE_PRIVATE);
    isFirstUse = sp.getBoolean("firstuse",true);
    if(!isFirstUse){//不是第一次使用
      intent2Activity(MainActivity.class);
    }else{
      initViews();
    }

  }

  private void initViews() {

    //获取inflater

    LayoutInflater inflater = LayoutInflater.from(this);
    views = new ArrayList<>();
    //添加引导页
    views.add(inflater.inflate(R.layout.one, null));
    views.add(inflater.inflate(R.layout.two, null));
    views.add(inflater.inflate(R.layout.three, null));

    View four = inflater.inflate(R.layout.four, null);

    //获取屏幕宽度和高度
    getAndroiodScreenProperty();
    //计算出弹窗的大小
    int pwidth = (int) (width * 0.85);
    int pheight = (int) (height * 0.85);
    //加载弹窗内容
    popupView = getLayoutInflater().inflate(R.layout.popup, null);

    //创建popupwindow
    popupWindow = new PopupWindow(popupView, pwidth, pheight, true);
    popupWindow.setAnimationStyle(R.style.PopupAnimation);//设置popupwindow动画

    //先监听，后放到vp中
    imageView = (ImageView)four.findViewById(R.id.imageView);
    //checkbox
    checkBox = (CheckBox)four.findViewById(R.id.checkBox);
    //开始按钮
    startbtn = (Button)four.findViewById(R.id.start);
    startbtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Log.d("startbtn","start LoginActivity");
        if(checkBox.isChecked()){
          //同意了以后才算第一次使用
          SharedPreferences sp = getSharedPreferences("demo",Context.MODE_PRIVATE);
          sp.edit().putBoolean("firstuse",false).commit();
          intent2Activity(LoginActivity.class);
        }else {
          //Toast.makeText(WelcomeActivity.this, "请先阅读免责声明～！", Toast.LENGTH_SHORT).show();
          AlertDialog dialog = new AlertDialog.Builder(WelcomeActivity.this)
                  .setTitle("提示")
                  .setIcon(getResources().getDrawable(R.drawable.warn2))
                  .setMessage("请仔细阅读并同意免责声明")
                  .setPositiveButton(R.string.comfirm, null)
                  .setNegativeButton(R.string.cancel, null)
                  .create();
          dialog.show();
        }
      }
    });

    views.add(four);//添加最后一个页面
    vpAdpter = new ViewPagerAdapter(views, this);
    vp = (ViewPager) findViewById(R.id.viewpager);
    vp.setAdapter(vpAdpter);

  }
  public void showPopwindow(View v){
    Log.d("popwindow", "showPopwindow: enter pop");
    popupWindow.showAtLocation(v, Gravity.CENTER,0,0);
  }

  public void getAndroiodScreenProperty() {
    WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(dm);
    width = dm.widthPixels;         // 屏幕宽度（像素）
    height = dm.heightPixels;       // 屏幕高度（像素）
    float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
    int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）

  }

}
